package com.zk.yygh.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zk.yygh.hosp.exception.HospitalException;
import com.zk.yygh.util.MD5;
import com.zk.yygh.hosp.result.Result;
import com.zk.yygh.hosp.service.HospitalSetService;
import com.zk.yygh.model.hosp.HospitalSet;
import com.zk.yygh.vo.hosp.HospitalSetQueryVo;
import com.zk.yygh.vo.order.SignInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;


/**
 * @author: 张凯
 * @date: 2022/5/11 - 18:54
 */
@Api("医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

   @ApiOperation("1.查询所有医院信息")
   @GetMapping("findAll")
   public Result findAllHospital(){
       List<HospitalSet> hospitalSetList = hospitalSetService.list();
//       try {
//           int i=1/0;
//       }catch (Exception e){
//           throw new HospitalException("！！出大异常了！！",222);
//       }
       return Result.ok(hospitalSetList);
   }

   @ApiOperation("2.删除指定医院信息")
   @DeleteMapping("{id}")
    public Result deleteHospitalById(@PathVariable("id") Long id){
       boolean flag = hospitalSetService.removeById(id);
       return Result.ok(flag);
   }

   @ApiOperation("3.条件查询带分页")
   @PostMapping("findPageHospitalSet/{current}/{limit}")
   public Result findPageHospitalSet(@PathVariable("current") long current,
                                     @PathVariable("limit") long limit,
                                     @RequestBody(required = false)HospitalSetQueryVo hospitalSetQueryVo){
       //创建分页插件
       Page<HospitalSet> page = new Page<>(current, limit);
       //获取条件
       String hosname = hospitalSetQueryVo.getHosname();
       String hoscode = hospitalSetQueryVo.getHoscode();
       //条件构造器
       QueryWrapper<HospitalSet> wrapper=new QueryWrapper<>();
       if (StringUtils.isNotBlank(hosname)){
           //根据医院名字进行模糊查询
           wrapper.like("hosname",hosname);
       }
       if (StringUtils.isNotBlank(hoscode)){
           //根据医院编号进行等值查询
           wrapper.eq("hoscode",hoscode);
       }
       //通过通用service进行带分页查询
       Page<HospitalSet> data = hospitalSetService.page(page, wrapper);
       return Result.ok(data);
   }

   @ApiOperation("4.医院添加")
   @PostMapping("saveHospitalSet")
   public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet){
       //设置其状态: 能够使用
       hospitalSet.setStatus(1);
       //设置其秘钥：MD5加密(当前时间戳及随机数)
       Random random = new Random();
       hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+random.nextInt(1000)));
       //写入数据库
       boolean flag = hospitalSetService.save(hospitalSet);
       if (flag){
           return Result.ok();
       }
       return Result.fail();
   }

   @ApiOperation("5.根据id获取医院设置")
    @GetMapping("getHospById/{id}")
    public Result getHospById(@PathVariable("id") Long id){
       HospitalSet hospitalSet = hospitalSetService.getById(id);
       return Result.ok(hospitalSet);
   }

   @ApiOperation("6.修改医院设置")
   @PostMapping("updateHosp")
   public Result updateHosp(@RequestBody HospitalSet hospitalSet){
       boolean flag = hospitalSetService.updateById(hospitalSet);
       if (flag){
           return Result.ok();
       }
       return Result.fail();
   }

   @ApiOperation("7.批量删除医院设置")
    @DeleteMapping("deleteBatchById")
    public Result deleteBatchById(@RequestBody List<Long> idList){
       boolean flag = hospitalSetService.removeByIds(idList);
       if (flag){
           return Result.ok();
       }
       return Result.fail();
   }

   @ApiOperation("8.改变医院的状态")
    @PutMapping("updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable("id") Long id,@PathVariable("status") Integer status){
       //根据id查询
       HospitalSet hospitalSet = hospitalSetService.getById(id);
       //设置该医院的状态: 锁定和解锁（0:1）
       hospitalSet.setStatus(status);
       hospitalSetService.updateById(hospitalSet);
       return Result.ok();
   }

   @ApiOperation("9.发送签名信息")
    @PutMapping("sendKey/{id}")
    public Result sendKey(@PathVariable("id") Long id){
       HospitalSet hospitalSet = hospitalSetService.getById(id);
       //获取医院编号及秘钥
       String hoscode = hospitalSet.getHoscode();
       String signKey = hospitalSet.getSignKey();
       //TODO 发送短信
       return Result.ok();
   }

    @ApiOperation(value = "获取下单引用的签名信息：远程调用")
    @GetMapping("inner/getSignInfoVo/{hoscode}")
    SignInfoVo getSignInfoVo(@PathVariable("hoscode") String hoscode){
        return hospitalSetService.getSignInfoVo(hoscode);
    }

}
